<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	
	public $components = array(
	        'Acl',
	        'Auth' => array(
	            'authorize' => array(
	                'Actions' => array('actionPath' => 'controllers')
	)
	),
	        'Session'
	);
	public $helpers = array('Html', 'Form', 'Session','Js');
	
	public function beforeFilter() {
		$this->_setLanguage();
		
		//Configure AuthComponent
		$this->Auth->allow('display');
		
		$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
		$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
		$this->Auth->loginRedirect = '/';
		
		$this->Session->write('Config.domain',Configure::read('Config.domain'));
		
	}
	
	public function beforeRender(){
		parent::beforeRender();
		$this->layout = 'panel';
	}
	
	/**
	 * 
	 * set language
	 */
	public function _setLanguage() {
		$this->locale = Configure::read('Config.language');
		$this->Session->write('Config.language', Configure::read('Config.language'));
	}
	
	/**
	*
	* Przekształcenie struktury Tree do wyświetlaneia w combobox lub na liscie
	* @param array $data // dane z zapytania $model->find('treahend');
	* @param string $model // nazwa modeluz którego pobrano dane
	* @param bool $full // true: zwraca array dodając jedynie wartosc depth; false: zwaraca listę id => name;
	*/
	protected function treeForCombobox($data,$model,$display=array('model'=>null,'name'=>null),$full=false){
		$parentCategories_ = $this->treeComboBox($data,$model,$display);
		if($full) return $parentCategories_;
		$parentCategories = array();
		foreach ($parentCategories_ as $key => $value) {
			$parentCategories[$value[$model]['id']] = $value[$display['model']][$display['name']];
		}
		return $parentCategories;
	}
	
	
	/**
	 *
	 * Dodaje wartość depth
	 * @param $data
	 * @param $model
	 */
	private function treeComboBox($data,$model,$display) {
		$result = array();
		if(!is_object($this->{$model})){
			$this->loadModel($model);
		}
		foreach ($data as $i) {
			$newData = $i;			
			$newData[$model]['depth'] = count($this->{$model}->getPath($i[$model]['id'])) - 1;
			$newData[$model][$display['name']] = str_repeat(" . . . . . . . ", $newData[$model]['depth']).$newData[$display['model']][$display['name']];
	
			unset($newData['children']);
			$result[] = $newData;
			if(isset($i['children']) && is_array($i['children'])) {
				$children = $this->treeComboBox($i['children'],$model,$display);
				foreach($children as $k) {
					$result[] = $k;
				}
			}
		}
		return $result;
	}
	
}
